package com.star.common;

import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author vietph
 */
public enum UserType {

    VIEWER((byte) 0, "Người dùng"), ADMIN((byte) 1, "Admin");
    byte id;
    String name;

    UserType(byte id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }
    static final Map<Integer, UserType> byId = Collections.synchronizedMap(new HashMap<Integer, UserType>());
    static final Map<String, UserType> byName = Collections.synchronizedMap(new HashMap<String, UserType>());

    static {
        for (UserType type : EnumSet.allOf(UserType.class)) {
            byId.put(type.getId(), type);
            byName.put(type.getName(), type);
        }
    }

    public static UserType getById(int id) throws IllegalArgumentException {
        UserType type = byId.get(id);
        if (type == null) {
            throw new IllegalArgumentException(String.format("User Type with ID %d does not exist.", id));
        }
        return type;
    }

}
